using CfgTable;
using IQIGame.Onigao.Framework;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class DynMainCityEntity : BaseDynMainCityEntity
    {
        public override int id => entityConfig.id; //主城实体没有服务器数据，也不存在克隆实体，所以id暂时以mcid代替
        public override int mcid => entityConfig.id;
        public override int mapSymbolCid => entityConfig.mapUIEntityCid;
        public override IReadOnlyList<int> offeredQuests
        {
            get
            {
                if (entityConfig.paramMap.TryGetValue(MainCityEntityDynParamType.QuestList, out var dynParam))
                {
                    return (dynParam as MainCityDynParamQuest).offerQuests;
                }
                return null;
            }
        }
        public MainCityEntityConfig entityConfig { get; protected set; }
        public CfgMainCityNpc cfgMainCityNpc { get; private set; }
        public CfgPhysicalEntity cfgEntity { get; private set; }
        public bool isActive { get; protected set; }
        public byte interactType { get; protected set; }
        public bool isObstacleActive { get; protected set; }
        public bool isCheckPoint { get; protected set; }
        public bool isCheckPointActive { get; protected set; }
        public List<Vector3Int> patrolPath { get; protected set; }
        public string behavScriptName { get; private set; }

        public DynMainCityEntity(MainCityEntityConfig entityConfig, MainCityNpcData npcSaveData)
        {
            this.entityConfig = entityConfig;
            this.cfgMainCityNpc = TableCenter.mainCityNpc.Get(entityConfig.npcCid);
            this.cfgEntity = TableCenter.physicalEntity.Get(cfgMainCityNpc.EntityId);
            this.isActive = entityConfig.isActive;
            this.interactType = entityConfig.interactType;
            this.isObstacleActive = entityConfig.isObstacleActive;
            this.position = entityConfig.position;
            this.dirEuler = entityConfig.initDirEuler;
            this.behavScriptName = entityConfig.behavScriptName;
            BaseMainCityEntityDynParam paramData;
            if (entityConfig.paramMap.TryGetValue(MainCityEntityDynParamType.CheckPoint, out paramData))
            {
                isCheckPoint = true;
                var checkPointParam = paramData as MainCityDynParamCheckPoint;
                this.isCheckPointActive = checkPointParam.isActive;
            }
            else if (entityConfig.paramMap.TryGetValue(MainCityEntityDynParamType.PatrolPath, out paramData))
            {
                var patrolParam = paramData as MainCityDynParamPatrolPath;
                patrolPath = patrolParam.patrolPath.Clone();
            }

            UpdateFromServerData(npcSaveData, out _, out _);
        }

        public void UpdateFromServerData(MainCityNpcData serverData, out bool isPos2DChanged, out bool isPosYChanged)
        {
            isPos2DChanged = false;
            isPosYChanged = false;
            if (serverData != null)
            {
                this.isActive = serverData.active;

                var p1 = position;
                var p2 = serverData.pos;

                isPos2DChanged = p1.x != p2.x || p1.z != p2.z;
                isPosYChanged = p1.y != p2.y;

                this.position = serverData.pos;
            }
        }
    }
}
